<?php
   //注册后端 前端传来name pwd 添加入数据库

   //获取前端传过来的参数
   $name = $_GET['name'];
   $pwd = $_GET['pwd'];

   $conn = mysqli_connect('localhost', 'root', 'root', '2008');
   mysqli_query($conn, "set charset 'utf8'");
   mysqli_query($conn, "set character set 'utf8'");

   //查询有没有相同的名字
   $sqlSelect = "select * from users where name='$name'";
   $resSelect = mysqli_query($conn,$sqlSelect);
   $count = mysqli_num_rows($resSelect);
   if($count > 0){
      echo json_encode('有相同的用户名了，请重新注册');
   }else{
       //sql添加语句
       $sql = "insert into users (name,pwd) values ('$name',$pwd)";
       $res = mysqli_query($conn,$sql);
       // $res = false;
       if($res){
          echo json_encode(array(
             'code' => 200,
             'body' => array(
                'msg' => '注册成功'
             )
             ));
       }else{
          echo json_encode(array(
             'code' => 201,
             'body' => array(
                'msg' => '网路出错，注册失败，请重试!'
             )
             ));
       }
   }
   
   ?>